package com.ssn.controller.admin;


import com.ssn.po.Tag;
import com.ssn.po.Type;
import com.ssn.service.ITagService;
import com.ssn.service.impl.TagServiceImpl;
import com.ssn.service.impl.TypeServiceImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TagController {

    @Resource
    private ITagService tagService;

    @GetMapping("/tags")
    public String tagPage(@PageableDefault(size = 5,direction = Sort.Direction.DESC) Pageable pageable,
                           Model model){
        model.addAttribute("page", tagService.listTag(pageable));
        return "/admin/tags";
    }
    @GetMapping("/tags/add")
    public String addPage(Model model){
        model.addAttribute("tag",new Tag());
        return "/admin/input-tag";
    }
    @GetMapping("/tags/{id}/input")
    public String editPage(@PathVariable Long id,Model model){
        Tag tag = tagService.getTag(id);
        model.addAttribute("tag",tag);
        return "/admin/input-tag";
    }


    @PostMapping("/tags/add")
    public String addTag(@Valid Tag tag,
                          BindingResult bindingResult,
                          RedirectAttributes attributes){
        if (bindingResult.hasErrors()){
             return "admin/input-tag";
        }
        Tag t = tagService.findTagByName(tag.getName());
        if (t != null){
            bindingResult.rejectValue("name","nameError","该标签已经存在，请勿重复添加！");
            return "/admin/input-tag";
        }
        tagService.saveTag(tag);
        attributes.addFlashAttribute("success",true);
        attributes.addFlashAttribute("message","新增标签成功！");
        return "redirect:/admin/tags";
    }

    @PostMapping("/tags/edit")
    public String editTag(@Valid Tag tag,
                          BindingResult bindingResult,
                          RedirectAttributes attributes){
        if (bindingResult.hasErrors()){
            return "admin/input-tag";
        }
        Tag t = tagService.findTagByName(tag.getName());
        if (t != null){
            bindingResult.rejectValue("name","nameError","该标签已经存在，请勿重复添加！");
            return "/admin/input-tag";
        }
        tagService.updateTag(tag.getId(),tag);
        attributes.addFlashAttribute("success",true);
        attributes.addFlashAttribute("message","修改标签成功！");
        return "redirect:/admin/tags";
    }

    @GetMapping("/tags/{id}/delete")
    public String deleteTag(@PathVariable Long id){
        tagService.deteleTag(id);
        return "redirect:/admin/tags";
    }

}
